﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using System.Windows.Forms;

namespace Swsk33.SimpleArcEngine.Dialog
{
	public partial class TextMarkDialog : Form
	{

		/// <summary>
		/// 传入地图控件
		/// </summary>
		private AxMapControl mapControl;

		public TextMarkDialog(AxMapControl mapControl)
		{
			this.mapControl = mapControl;
			InitializeComponent();
		}

		/// <summary>
		/// 设定字段下拉框内容
		/// </summary>
		private void setFileds()
		{
			fieldBox.Items.Clear();
			IFeatureLayer featureLayer = (FeatureLayer)mapControl.get_Layer(layerBox.SelectedIndex);
			for (int i = 0; i < featureLayer.FeatureClass.Fields.FieldCount; i++)
			{
				// 排除Shape字段
				if (featureLayer.FeatureClass.Fields.Field[i].Name.Equals("Shape"))
				{
					continue;
				}
				fieldBox.Items.Add(featureLayer.FeatureClass.Fields.Field[i].Name);
			}
			if (fieldBox.Items.Count > 0)
			{
				fieldBox.SelectedIndex = 0;
			}
		}

		private void TextMarkDialog_Load(object sender, System.EventArgs e)
		{
			for (int i = 0; i < mapControl.LayerCount; i++)
			{
				layerBox.Items.Add(mapControl.get_Layer(i).Name);
			}
			if (layerBox.Items.Count > 0)
			{
				layerBox.SelectedIndex = 0;
				setFileds();
			}
		}

		private void layerBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			setFileds();
		}

		private void ok_Click(object sender, System.EventArgs e)
		{
			// 创建几何图层
			IGeoFeatureLayer geoFeatureLayer = (IGeoFeatureLayer)mapControl.get_Layer(layerBox.SelectedIndex);
			IAnnotateLayerPropertiesCollection propertiesCollection = geoFeatureLayer.AnnotationProperties;
			propertiesCollection.Clear();
			// 创建图层属性引擎并设定表达式
			ILabelEngineLayerProperties layerProperties = new LabelEngineLayerPropertiesClass();
			layerProperties.Expression = "[" + fieldBox.SelectedItem.ToString() + "]";
			// 定义文字符号样式
			ITextSymbol textSymbol = layerProperties.Symbol;
			textSymbol.VerticalAlignment = esriTextVerticalAlignment.esriTVACenter;
			textSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHACenter;
			textSymbol.Size = 9;
			textSymbol.Font.Name = "宋体";
			// 开启图层文字标记显示
			geoFeatureLayer.DisplayAnnotation = true;
			// 设定显示位置参数
			IPointPlacementPriorities placementPriorities = new PointPlacementPriorities();
			placementPriorities.AboveCenter = 1;
			placementPriorities.AboveRight = 0;
			layerProperties.BasicOverposterLayerProperties.PointPlacementPriorities = placementPriorities;
			propertiesCollection.Add((IAnnotateLayerProperties)layerProperties);
			mapControl.Refresh();
			Close();
		}
	}
}